﻿using System;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using AzureQueue;

namespace WebPages.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        void SendValidatationEmail()
        {
            var userName = RegisterUser.UserName;
            var password = RegisterUser.Password;
            var emailAddress = RegisterUser.Email;

            // Lets get the user's id
            var userId = (Guid)Membership.GetUser(userName).ProviderUserKey;
            
            // Now lets create an email message
            var emailMessage = new StringBuilder();
            var header = Resources.Email.EmailHeader.Replace("{0}", "Thank you for creating a MyApp account!");
            emailMessage.Append(header);
            emailMessage.Append("Please click the below link to activate your MyApp account and complete your account registration:<br /><br />");
            var url = string.Format("http://www.MyApp.com/Account/ActivateAccount.aspx?Id={0}", userId);
            emailMessage.Append(string.Format("<a href='{0}'>{0}</a><br /><br />", url));
            emailMessage.Append("If your e-mail client does not allow links then copy & paste the above url into your browser address bar.");
            emailMessage.Append(string.Format("<br /><br />Username: <span class=\"username\">{0}</span><br />Password: <span class=\"password\">{1}</span>", userName, password));
            var footer = String.Format(Resources.Email.EmailFooter, "This e-mail was sent because someone registered an user account at <a href=\"http://www.MyApp.com\">www.MyApp.com</a> using this e-mail address.");
            emailMessage.Append(footer);
            var fromEmail = ConfigurationManager.AppSettings["fromEmail"];

            // Send the email
            var queueDataSource = new AzureQueueDataSource();
            queueDataSource.SendEmail(fromEmail, emailAddress, "Activate your MyApp account", emailMessage.ToString());
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            SendValidatationEmail();
        }

    }
}
